# Loading required packages
library(tidyverse); library(ggplot2)

##############
# FIGURE A.1 #
##############
# Loading the Senator press release data
load("senator_press_data.RData")

# Overall percentage of press releases referencing process or obstruction, according to string search
round(sum(senator_press_obstruction$any.discussion)/length(senator_press_obstruction$any.discussion),3)

# Table of total number of press releases for each nomination
press.for.each.nomination <- table(senator_press_obstruction$nomination)

# Table of total number of press releases for each nomination that reference process or obstruction, according to string search
combined.by.nomination.press <- table(senator_press_obstruction$nomination[senator_press_obstruction$any.discussion == 1])

# Turning table of references of obstruction into data frame for plotting
press.plot.df <- data.frame(combined.by.nomination.press)
# Descriptive column names
colnames(press.plot.df) <- c("Nomination","Count")
# Percentage of press releases for each nomination that reference process or obstruction
press.plot.df$Percentage <- as.numeric(round(combined.by.nomination.press/press.for.each.nomination,2))

# Reordering to plot nominations by time
press.plot.df <- press.plot.df[c(1,8,9,10,6,3,4,7,2,5),]
# Making nomination a factor for plotting
press.plot.df$Nomination <- str_to_sentence(as.character(press.plot.df$Nomination))
press.plot.df$Nomination <- factor(press.plot.df$Nomination, levels = press.plot.df$Nomination)

# Plotting
ggplot(data=press.plot.df, aes(x=Nomination, y=Count)) +
  geom_bar(stat="identity", position=position_dodge()) + 
  geom_text(aes(label=Percentage), vjust=-1) +
  scale_x_discrete(limits=c("Alito", "Miers", "Roberts","Sotomayor","Kagan","Garland","Gorsuch","Kavanaugh","Barrett","Jackson")) +
  scale_y_continuous(limits=c(0,200), breaks=seq(0, 200, 50)) +
  theme_classic() +
  labs(x="Nomination", y="Number of Press Releases About Obstruction", fill="")

##############
# FIGURE A.2 #
##############
# Loading the broadcast news data
load("broadcast_news_data.RData")

# Overall percentage of press releases referencing process or obstruction, according to string search
round(sum(broadcast_news_obstruction$any.discussion)/length(broadcast_news_obstruction$any.discussion),3)

# Table of total number of transcripts for each nomination
transcripts.for.each.nomination <- table(broadcast_news_obstruction$nomination)

# Table of total number of transcripts for each nomination that reference process or obstruction, according to string search
combined.by.nomination <- table(broadcast_news_obstruction$nomination[broadcast_news_obstruction$any.discussion == 1])

# Turning table of references of obstruction into data frame for plotting
transcript.plot.df <- data.frame(combined.by.nomination)
# Descriptive column names
colnames(transcript.plot.df) <- c("Nomination","Count")
# Percentage of press releases for each nomination that reference process or obstruction
transcript.plot.df$Percentage <- as.numeric(round(combined.by.nomination/transcripts.for.each.nomination,2))

# Reordering to plot nominations by time
transcript.plot.df <- transcript.plot.df[c(1,8,9,10,6,3,4,7,2,5),]

ggplot(data=transcript.plot.df, aes(x=Nomination, y=Count)) +
  geom_bar(stat="identity", position=position_dodge()) + 
  geom_text(aes(label=Percentage), vjust=-1) +
  scale_x_discrete(limits=c("Alito", "Miers", "Roberts","Sotomayor","Kagan","Garland","Gorsuch","Kavanaugh","Barrett","Jackson")) +
  scale_y_continuous(limits=c(0,700), breaks=seq(0, 700, 100)) +
  theme_classic() +
  labs(x="Nomination", y="Number of Broadcast News Segments About Obstruction", fill="") 
